home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Sound / LAME / WarpOS / src / mpglib / interface.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-18  |  11.8 KB  |  578 lines

  1. /* $Id: interface.c,v 1.36 2001/06/18 18:51:52 markt Exp $ */
  2.  
  3. #ifdef HAVE_CONFIG_H
  4. # include <config.h>
  5. #endif
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. #include "common.h"
  11. #include "interface.h"
  12. #include "tabinit.h"
  13. #include "layer3.h"
  14. #include "VbrTag.h"
  15.  
  16. #ifdef USE_LAYER_1
  17.     #include "layer1.h"
  18. #endif
  19.  
  20. #ifdef USE_LAYER_2
  21.     #include "layer2.h"
  22. #endif
  23.  
  24. #ifdef WITH_DMALLOC
  25. #include <dmalloc.h>
  26. #endif
  27.  
  28.  
  29. BOOL InitMP3( PMPSTR mp) 
  30. {
  31.     memset(mp,0,sizeof(MPSTR));
  32.  
  33.     mp->framesize = 0;
  34.         mp->num_frames = 0;
  35.         mp->vbr_header=0;
  36.     mp->header_parsed=0;
  37.     mp->side_parsed=0;
  38.     mp->data_parsed=0;
  39.     mp->free_format=0;
  40.     mp->old_free_format=0;
  41.     mp->ssize = 0;
  42.     mp->dsize=0;
  43.     mp->fsizeold = -1;
  44.     mp->bsize = 0;
  45.     mp->head = mp->tail = NULL;
  46.     mp->fr.single = -1;
  47.     mp->bsnum = 0;
  48.     wordpointer = mp->bsspace[mp->bsnum] + 512;
  49.     mp->synth_bo = 1;
  50.     mp->look_for_xing = 0;
  51.  
  52.     make_decode_tables(32767);
  53.  
  54.     init_layer3(SBLIMIT);
  55.  
  56. #ifdef USE_LAYER_2
  57.     init_layer2();
  58. #endif
  59.  
  60.     return !0;
  61. }
  62.  
  63. void ExitMP3( PMPSTR mp)
  64. {
  65.     struct buf *b,*bn;
  66.     
  67.     b = mp->tail;
  68.     while(b) {
  69.         free(b->pnt);
  70.         bn = b->next;
  71.         free(b);
  72.         b = bn;
  73.     }
  74. }
  75.  
  76. static struct buf *addbuf( PMPSTR mp, unsigned char *buf,int size)
  77. {
  78.     struct buf *nbuf;
  79.  
  80.     nbuf = (struct buf*) malloc( sizeof(struct buf) );
  81.     if(!nbuf) {
  82.         fprintf(stderr,"Out of memory!\n");
  83.         return NULL;
  84.     }
  85.     nbuf->pnt = (unsigned char*) malloc((size_t)size);
  86.     if(!nbuf->pnt) {
  87.         free(nbuf);
  88.         return NULL;
  89.     }
  90.     nbuf->size = size;
  91.     memcpy(nbuf->pnt,buf,(size_t)size);
  92.     nbuf->next = NULL;
  93.     nbuf->prev = mp->head;
  94.     nbuf->pos = 0;
  95.  
  96.     if(!mp->tail) {
  97.         mp->tail = nbuf;
  98.     }
  99.     else {
  100.       mp->head->next = nbuf;
  101.     }
  102.  
  103.     mp->head = nbuf;
  104.     mp->bsize += size;
  105.  
  106.     return nbuf;
  107. }
  108.  
  109. void remove_buf(PMPSTR mp)
  110. {
  111.   struct buf *buf = mp->tail;
  112.   
  113.   mp->tail = buf->next;
  114.   if(mp->tail)
  115.     mp->tail->prev = NULL;
  116.   else {
  117.     mp->tail = mp->head = NULL;
  118.   }
  119.   
  120.   free(buf->pnt);
  121.   free(buf);
  122.  
  123. }
  124.  
  125. static int read_buf_byte(PMPSTR mp)
  126. {
  127.     unsigned int b;
  128.  
  129.     int pos;
  130.  
  131.     
  132.     pos = mp->tail->pos;
  133.     while(pos >= mp->tail->size) {
  134.             remove_buf(mp);
  135.         if(!mp->tail) {
  136.             fprintf(stderr,"Fatal error! tried to read past mp buffer\n");
  137.             exit(1);
  138.         }
  139.         pos = mp->tail->pos;
  140.     }
  141.  
  142.     b = mp->tail->pnt[pos];
  143.     mp->bsize--;
  144.     mp->tail->pos++;
  145.     
  146.  
  147.     return b;
  148. }
  149.  
  150.  
  151.  
  152. static void read_head(PMPSTR mp)
  153. {
  154.     unsigned long head;
  155.  
  156.     head = read_buf_byte(mp);
  157.     head <<= 8;
  158.     head |= read_buf_byte(mp);
  159.     head <<= 8;
  160.     head |= read_buf_byte(mp);
  161.     head <<= 8;
  162.     head |= read_buf_byte(mp);
  163.  
  164.     mp->header = head;
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. void copy_mp(PMPSTR mp,int size,unsigned char *ptr) 
  173. {
  174.   int len = 0;
  175.  
  176.   while(len < size) {
  177.     int nlen;
  178.     int blen = mp->tail->size - mp->tail->pos;
  179.     if( (size - len) <= blen) {
  180.       nlen = size-len;
  181.     }
  182.     else {
  183.       nlen = blen;
  184.     }
  185.     memcpy(ptr+len,mp->tail->pnt+mp->tail->pos,(size_t)nlen);
  186.     len += nlen;
  187.     mp->tail->pos += nlen;
  188.     mp->bsize -= nlen;
  189.     if(mp->tail->pos == mp->tail->size) {
  190.       remove_buf(mp);
  191.     }
  192.   }
  193. }
  194.  
  195.  
  196.  
  197.  
  198. // traverse mp data structure without changing it
  199. // (just like sync_buffer)
  200. // pull out 48 bytes
  201. // call vbr header check code from LAME
  202. // if we find a header, parse it and also compute the VBR header size
  203. // if no header, do nothing.
  204. //
  205. // bytes = number of bytes before MPEG header.  skip this many bytes
  206. // before starting to read
  207. // return value: number of bytes in VBR header, including syncword
  208. int check_vbr_header(PMPSTR mp,int bytes)
  209. {
  210.   int i,pos;
  211.   struct buf *buf=mp->tail;
  212.   unsigned char xing[48];
  213.   VBRTAGDATA pTagData;
  214.  
  215.   pos = buf->pos;
  216.   // skip to valid header
  217.   for (i=0; i<bytes; ++i) {
  218.     while(pos >= buf->size) {
  219.       buf  = buf->next;
  220.       pos = buf->pos;
  221.       if(!buf)     return -1; /* fatal error */
  222.     }
  223.     ++pos;
  224.   }
  225.   // now read 48 bytes
  226.   for (i=0; i<48; ++i) {
  227.     while(pos >= buf->size) {
  228.       buf  = buf->next;
  229.       pos = buf->pos;
  230.       if(!buf)     return -1; /* fatal error */
  231.     }
  232.     xing[i] = buf->pnt[pos];
  233.     ++pos;
  234.   }
  235.  
  236.   /* check first 48 bytes for Xing header */
  237.   mp->vbr_header = GetVbrTag(&pTagData,xing);
  238.   if (mp->vbr_header) {
  239.     mp->num_frames=pTagData.frames;
  240.     // fprintf(stderr,"\rmpglib: Xing VBR header dectected.  MP3 file has %i frames\n", pTagData.frames);
  241.     return pTagData.headersize;
  242.   }
  243.   return 0;
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252. int sync_buffer(PMPSTR mp,int free_match) 
  253. {
  254.   /* traverse mp structure without modifing pointers, looking
  255.    * for a frame valid header.
  256.    * if free_format, valid header must also have the same
  257.    * samplerate.   
  258.    * return number of bytes in mp, before the header
  259.    * return -1 if header is not found
  260.    */
  261.   unsigned int b[4]={0,0,0,0};
  262.   int i,h,pos;
  263.   struct buf *buf=mp->tail;
  264.  
  265.   pos = buf->pos;
  266.   for (i=0; i<mp->bsize; i++) {
  267.     /* get 4 bytes */
  268.     
  269.     b[0]=b[1]; b[1]=b[2]; b[2]=b[3];
  270.     while(pos >= buf->size) {
  271.       buf  = buf->next;
  272.       pos = buf->pos;
  273.       if(!buf) {
  274.     return -1;
  275.     /* not enough data to read 4 bytes */
  276.       }
  277.     }
  278.     b[3] = buf->pnt[pos];
  279.     ++pos;
  280.  
  281.     if (i>=3) {
  282.         struct frame *fr = &mp->fr;
  283.     unsigned long head;
  284.  
  285.     head = b[0];
  286.     head <<= 8;
  287.     head |= b[1];
  288.     head <<= 8;
  289.     head |= b[2];
  290.     head <<= 8;
  291.     head |= b[3];
  292.     h = head_check(head,fr->lay);
  293.  
  294.     if (h && free_match) {
  295.       /* just to be even more thorough, match the sample rate */
  296.       int mode,stereo,sampling_frequency,mpeg25,lsf;
  297.  
  298.       if( head & (1<<20) ) {
  299.         lsf = (head & (1<<19)) ? 0x0 : 0x1;
  300.         mpeg25 = 0;
  301.       }
  302.       else {
  303.         lsf = 1;
  304.         mpeg25 = 1;
  305.       }
  306.  
  307.       mode      = ((head>>6)&0x3);
  308.       stereo    = (mode == MPG_MD_MONO) ? 1 : 2;
  309.  
  310.       if(mpeg25) 
  311.         sampling_frequency = 6 + ((head>>10)&0x3);
  312.       else
  313.         sampling_frequency = ((head>>10)&0x3) + (lsf*3);
  314.       h = ((stereo==fr->stereo) && (lsf==fr->lsf) && (mpeg25==fr->mpeg25) && 
  315.                  (sampling_frequency == fr->sampling_frequency));
  316.     }
  317.  
  318.     if (h) {
  319.       return i-3;
  320.     }
  321.     }
  322.   }
  323.   return -1;
  324. }
  325.  
  326.  
  327.  
  328.  
  329.  
  330. int decodeMP3( PMPSTR mp,unsigned char *in,int isize,char *out,
  331.         int osize,int *done)
  332. {
  333.     int i,iret,bits,bytes;
  334.  
  335.     if(osize < 4608) {
  336.         fprintf(stderr,"To less out space\n");
  337.         return MP3_ERR;
  338.     }
  339.  
  340.     if(in) {
  341.         if(addbuf(mp,in,isize) == NULL) {
  342.             return MP3_ERR;
  343.         }
  344.     }
  345.  
  346.  
  347.     /* First decode header */
  348.     if(!mp->header_parsed) {
  349.  
  350.         if (mp->fsizeold==-1 || mp->look_for_xing) {
  351.             int vbrbytes;
  352.  
  353.             /* This is the very first call.   sync with anything */
  354.         /* bytes= number of bytes before header */
  355.             bytes=sync_buffer(mp,0); 
  356.  
  357.             /* now look for Xing VBR header */
  358.         if (mp->bsize >= bytes+48 ) {
  359.             /* vbrbytes = number of bytes in entire vbr header */
  360.             vbrbytes=check_vbr_header(mp,bytes);
  361.         } else {
  362.             /* not enough data to look for Xing header */
  363.             return MP3_NEED_MORE;
  364.         }
  365.  
  366.         if (mp->vbr_header) {
  367.             /* do we have enough data to parse entire Xing header? */
  368.             if (bytes+vbrbytes > mp->bsize) return MP3_NEED_MORE;
  369.             
  370.             /* read in Xing header.  Buffer data in case it
  371.              * is used by a non zero main_data_begin for the next
  372.              * frame, but otherwise dont decode Xing header */
  373.             for (i=0; i<vbrbytes+bytes; ++i) read_buf_byte(mp);
  374.             /* now we need to find another syncword */
  375.             /* just return and make user send in more data */
  376.             return MP3_NEED_MORE;
  377.         }
  378.             }else{
  379.             /* match channels, samplerate, etc, when syncing */
  380.                 bytes=sync_buffer(mp,1);
  381.         }
  382.  
  383.         if (bytes<0) return MP3_NEED_MORE;
  384.         if (bytes>0) {
  385.         /* there were some extra bytes in front of header.
  386.          * bitstream problem, but we are now resynced 
  387.          * should try to buffer previous data in case new
  388.          * frame has nonzero main_data_begin, but we need
  389.          * to make sure we do not overflow buffer
  390.          */
  391.         int size;
  392.         fprintf(stderr,"bitstream problem: resyncing...\n");
  393.         mp->old_free_format=0;
  394.         
  395.         /* skip some bytes, buffer the rest */
  396.         size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
  397.         
  398.         if (size > MAXFRAMESIZE) {
  399.             /* wordpointer buffer is trashed.  probably cant recover, but try anyway */
  400.             fprintf(stderr,"mpglib: wordpointer trashed.  size=%i (%i)  bytes=%i \n",
  401.                 size,MAXFRAMESIZE,bytes);          
  402.             size=0;
  403.             wordpointer = mp->bsspace[mp->bsnum]+512;
  404.         }
  405.         
  406.         /* buffer contains 'size' data right now 
  407.            we want to add 'bytes' worth of data, but do not 
  408.            exceed MAXFRAMESIZE, so we through away 'i' bytes */
  409.         i = (size+bytes)-MAXFRAMESIZE;
  410.         for (; i>0; --i) {
  411.             --bytes;
  412.             read_buf_byte(mp);
  413.         }
  414.         
  415.         copy_mp(mp,bytes,wordpointer);
  416.         mp->fsizeold += bytes;
  417.         }
  418.         
  419.         read_head(mp);
  420.         decode_header(&mp->fr,mp->header);
  421.         mp->header_parsed=1;
  422.         mp->framesize = mp->fr.framesize;
  423.         mp->free_format = (mp->framesize==0);
  424.         
  425.         if(mp->fr.lsf)
  426.         mp->ssize = (mp->fr.stereo == 1) ? 9 : 17;
  427.         else
  428.         mp->ssize = (mp->fr.stereo == 1) ? 17 : 32;
  429.         if (mp->fr.error_protection) 
  430.         mp->ssize += 2;
  431.         
  432.         mp->bsnum = 1-mp->bsnum; /* toggle buffer */
  433.         wordpointer = mp->bsspace[mp->bsnum] + 512;
  434.         bitindex = 0;
  435.         
  436.         /* for very first header, never parse rest of data */
  437.         if (mp->fsizeold==-1)
  438.         return MP3_NEED_MORE;
  439.     }
  440.     
  441.     /* now decode side information */
  442.     if (!mp->side_parsed) {
  443.  
  444.         /* Layer 3 only */
  445.         if (mp->fr.lay==3)
  446.         {
  447.                 if (mp->bsize < mp->ssize) 
  448.           return MP3_NEED_MORE;
  449.  
  450.         copy_mp(mp,mp->ssize,wordpointer);
  451.  
  452.         if(mp->fr.error_protection)
  453.           getbits(16);
  454.         bits=do_layer3_sideinfo(&mp->fr);
  455.         /* bits = actual number of bits needed to parse this frame */
  456.         /* can be negative, if all bits needed are in the reservoir */
  457.         if (bits<0) bits=0;
  458.  
  459.         /* read just as many bytes as necessary before decoding */
  460.         mp->dsize = (bits+7)/8;
  461.  
  462.         /* this will force mpglib to read entire frame before decoding */
  463.         /* mp->dsize= mp->framesize - mp->ssize;*/
  464.  
  465.         }
  466.  
  467.         else
  468.         {
  469.             /* Layers 1 and 2 */
  470.  
  471.             /* check if there is enough input data */
  472.             if(mp->fr.framesize > mp->bsize)
  473.                 return MP3_NEED_MORE;
  474.  
  475.             /* takes care that the right amount of data is copied into wordpointer */
  476.             mp->dsize=mp->fr.framesize;
  477.             mp->ssize=0;
  478.         }
  479.  
  480.         mp->side_parsed=1;
  481.     }
  482.  
  483.     /* now decode main data */
  484.     iret=MP3_NEED_MORE;
  485.     if (!mp->data_parsed ) {
  486.             if(mp->dsize > mp->bsize) {
  487.                 return MP3_NEED_MORE;
  488.         }
  489.  
  490.         copy_mp(mp,mp->dsize,wordpointer);
  491.  
  492.         *done = 0;
  493.  
  494.         //do_layer3(&mp->fr,(unsigned char *) out,done);
  495.         switch (mp->fr.lay)
  496.         {
  497. #ifdef USE_LAYER_1
  498.             case 1:
  499.                 if(mp->fr.error_protection)
  500.                     getbits(16);
  501.  
  502.                 do_layer1(mp,(unsigned char *) out,done);
  503.             break;
  504. #endif
  505. #ifdef USE_LAYER_2
  506.             case 2:
  507.                 if(mp->fr.error_protection)
  508.                     getbits(16);
  509.  
  510.                 do_layer2(mp,(unsigned char *) out,done);
  511.             break;
  512. #endif
  513.             case 3:
  514.                 do_layer3(mp,(unsigned char *) out,done);
  515.             break;
  516.             default:
  517.                 fprintf(stderr,"invalid layer %d\n",mp->fr.lay);
  518.         }
  519.  
  520.         wordpointer = mp->bsspace[mp->bsnum] + 512 + mp->ssize + mp->dsize;
  521.  
  522.         mp->data_parsed=1;
  523.         iret=MP3_OK;
  524.     }
  525.  
  526.  
  527.     /* remaining bits are ancillary data, or reservoir for next frame 
  528.      * If free format, scan stream looking for next frame to determine
  529.      * mp->framesize */
  530.     if (mp->free_format) {
  531.       if (mp->old_free_format) {
  532.         /* free format.  bitrate must not vary */
  533.         mp->framesize=mp->fsizeold_nopadding + (mp->fr.padding);
  534.       }else{
  535.         bytes=sync_buffer(mp,1);
  536.         if (bytes<0) return iret;
  537.         mp->framesize = bytes + mp->ssize+mp->dsize;
  538.         mp->fsizeold_nopadding= mp->framesize - mp->fr.padding;
  539.         /*
  540.         fprintf(stderr,"freeformat bitstream:  estimated bitrate=%ikbs  \n",
  541.             8*(4+mp->framesize)*freqs[mp->fr.sampling_frequency]/
  542.             (1000*576*(2-mp->fr.lsf)));
  543.         */
  544.       }
  545.     }
  546.  
  547.     /* buffer the ancillary data and reservoir for next frame */
  548.     bytes = mp->framesize-(mp->ssize+mp->dsize);
  549.     if (bytes > mp->bsize) {
  550.       return iret;
  551.     }
  552.  
  553.     if (bytes>0) {
  554.       int size;
  555.       copy_mp(mp,bytes,wordpointer);
  556.       wordpointer += bytes;
  557.  
  558.       size = (int) (wordpointer - (mp->bsspace[mp->bsnum]+512));
  559.       if (size > MAXFRAMESIZE) {
  560.         fprintf(stderr,"fatal error.  MAXFRAMESIZE not large enough.\n");
  561.       }
  562.  
  563.     }
  564.  
  565.     /* the above frame is completey parsed.  start looking for next frame */
  566.     mp->fsizeold = mp->framesize;
  567.     mp->old_free_format = mp->free_format;
  568.     mp->framesize =0;
  569.     mp->header_parsed=0;
  570.     mp->side_parsed=0;
  571.     mp->data_parsed=0;
  572.  
  573.     return iret;
  574. }
  575.  
  576.     
  577.  
  578.